<?php echo $this->element('displayHeader'); ?>
<?php echo $ajax->form('DRH', 'post', array('update'=>'content', 'indicator' => 'indicator', 'url' => array('action' => 'save')));?>
<div class="toolbar">
    <div id="tLeft">
        <?php if ($mode == 'add') : ?>
            <?php echo $this->element('toolbar', array('buttons' => array('save', 'clear', 'back'))); ?>
        <?php else :  // mode edit ?>
            <?php echo $this->element('toolbar', array('buttons' => array('add', 'save', array('print' => array('model' => 'DRH')), 'back'))); ?>
        <?php endif; ?>
    </div>
    <div id="tRight"><?php echo $this->element('neighbors', array('model' => 'DRH', 'title' => 'id')); ?></div>
</div>

<?php echo $form->input('DRH.id'); ?>
<?php echo $form->hidden('mode', array('value' => $mode)); ?>

<?php if ($mode == 'add' && $dispatches == null) : ?>
<script type="text/javascript">
    $j(function() {
        alert("There is no Dispatch to returned");
       $j('.save').hide();
    });
</script>
<?php endif; ?>
<?php if ($mode == 'add' && !empty($this->data['Dispatch'])) {
    echo $form->hidden('dispatch_status_id', array('value' => $this->data['Dispatch']['dispatch_status_id']));
} ?>
<fieldset>
    <table>
        <tr>
            <td width="180px"><?php echo $form->label('DRH.return_no', 'Return no'); ?></td>
            <td width="300px">
                <?php if ($mode == 'edit') : ?>
                <span id="return_no"><?php echo $this->data['DRH']['return_no'] ?></span>
                <?php endif; ?>
                &nbsp;
            </td>
            <td width="180px"><?php echo $form->label('DRH.dispatch_id', 'Dispatch no.', array('class' => 'required')); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->select('DRH.dispatch_id', $dispatches, $this->data['Dispatch']['id'], array('tabindex' => 5)); ?>
                <?php else : ?>
                <span id="dispatch_no"><?php echo $this->data['Dispatch']['dispatch_no'] ?></span>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('DRH.return_date', 'Return date'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $html->tag('span', $today); ?>
                    <?php echo $form->hidden('DRH.return_date', array('id' => 'return_date', 'value' => $today)); ?>
                <?php else : ?>
                <span id="return_date"><?php echo $this->data['DRH']['return_date'] ?></span>
                <?php endif; ?>
            </td>
            <td><?php echo $form->label('DRH.warehouse_id', 'Warehouse'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->hidden('DRH.warehouse_id', array('value' => $this->data['Dispatch']['warehouse_id'])); ?>
                    <span id="DRHWarehouseName"><?php if (!empty($this->data)) echo $this->data['Warehouse']['name']; ?></span>
                <?php else : ?>
                    <?php echo $this->data['Warehouse']['name'] ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('DRH.return_by', 'Return by'); ?></td>
            <td>
                <?php if($mode == 'add') : ?>
                    <?php echo $html->tag('span', $return_by); ?>
                    <?php echo $form->hidden('DRH.return_by', array('value' => $return_by)); ?>
                <?php else : ?>
                    <?php echo $html->tag('span', $this->data['DRH']['return_by']); ?>
                <?php endif; ?>
            </td>
            <td><?php echo $form->label('DRH.customer_id', 'Customer'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->hidden('DRH.customer_id', array('value' => $this->data['Dispatch']['customer_id'])); ?>
                    <span id="DRHCustomerName"><?php if (!empty($this->data)) echo $this->data['Customer']['name']; ?></span>
                <?php else : ?>
                    <?php echo $this->data['Customer']['name'] ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><?php echo $form->label('DRH.site_id', 'Site'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->hidden('DRH.site_id', array('value' => $this->data['Dispatch']['site_id'])); ?>
                    <span id="DRHSiteName"><?php if (!empty($this->data)) echo $this->data['Site']['name']; ?></span>
                <?php else : ?>
                    <?php echo $this->data['Site']['name'] ?>
                <?php endif; ?>
            </td>
        </tr>
    </table>
</fieldset>

<fieldset>
    <table class="data">
        <thead>
            <tr>
                <th class="no">No</th>
                <th width="130px">Part no.</th>
                <th>Description</th>
                <?php if ($mode == 'add') : ?>
                <th width="100px">Location</th>
                <?php endif; ?>
                <th width="100px">Return Qty</th>
                <th width="50px">Unit</th>
                <th width="50px">Index</th>
            </tr>
        </thead>
        <?php if (empty($this->data)) : ?>
        <tbody id="productRow">
            <tr class="odd-cell">
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </tbody>
        <?php elseif ($mode == 'add') : $count = count($this->data['CustStockIn'])?>
        <tbody id="tbodyDR">
                <?php for($i = 0; $i < $count; ++$i) : ?>
            <tr class="<?php echo (($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'; ?>">
                <td><span><?php echo $i + 1; ?></span></td>
                <td>
                    <?php echo $form->input("DR.{$i}.id"); ?>
                    <?php echo $form->hidden("DR.{$i}.product_id", array('value' => $this->data['CustStockIn'][$i]['product_id'])); ?>
                    <?php echo $form->hidden("DR.{$i}.cust_product_site_id", array('value' => $this->data['CustStockIn'][$i]['cust_product_site_id'])); ?>
                    <?php echo $form->hidden("DR.{$i}.return_qty", array('value' => $this->data['CustStockIn'][$i]['deliver_qty'] - $this->data['CustStockIn'][$i]['stock_in_qty'])); ?>
                    <?php echo $form->hidden("DR.{$i}.cust_stock_in_id", array('value' => $this->data['CustStockIn'][$i]['id'])); ?>
                    <?php echo $html->link($this->data['CustStockIn'][$i]['Product']['part_no'],
                    '/products/view/' . $this->data['CustStockIn'][$i]['product_id'] . '/' . strtotime('now'),
                    array('target' => '_blank')); ?>
                </td>
                <td><?php echo $this->data['CustStockIn'][$i]['Product']['text']; ?></td>
                <td>
                    <?php echo $form->select("DR.{$i}.location_id", $locations, null); ?>
                    <div id="DR<?php echo $i; ?>SeeIndex" class="caption" style="display: none;">(See index)</div>
                </td>
                <td class="text-right"><?php echo $this->data['CustStockIn'][$i]['deliver_qty'] - $this->data['CustStockIn'][$i]['stock_in_qty']; ?></td>
                <td class="center"><?php echo $this->data['CustStockIn'][$i]['Product']['Unit']['title']; ?></td>
                <td id="tdIndex<?php echo $i; ?>" class="center">
                    <?php echo $html->link($html->image('icon/popup.png', array('title' => 'Index', 'alt' => 'Index')), "#", array('id' => "DR{$i}Index"), null, false); ?>
                    <div id="Index<?php echo $i; ?>" style="display: none;">
                        <table class="data">
                            <thead>
                                <tr>
                                    <th width="35px">No</th>
                                    <th width="110px">Location</th>
                                    <th>Serial no.</th>
                                    <th>Other Information</th>
                                    <th width="70px">price</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $ps_count = count($this->data['CustStockIn'][$i]['ProductSerial']);
                                for($j = 0; $j < $ps_count; ++$j) : ?>
                                <tr class="<?php echo $j % 2 == 0? 'even-cell': 'odd-cell'; ?>">
                                    <td class="center"><?php echo $j + 1; ?></td>
                                    <td>
                                        <input type="hidden" name="data[DR][<?php echo $i; ?>][ProductSerial][<?php echo $j; ?>][id]" value ="<?php echo $this->data['CustStockIn'][$i]['ProductSerial'][$j]['id']; ?>"/>
                                        <?php echo $form->select("DR.{$i}.ProductSerial.{$j}.location_id", $locations, $this->data['CustStockIn'][$i]['ProductSerial'][$j]['location_id'], array('name' => "data[DR][{$i}][ProductSerial][{$j}][location_id]", 'id' => "DR{$i}ProductSerial{$j}LocationId")); ?>
                                    </td>
                                    <td><?php echo !empty($this->data['CustStockIn'][$i]['ProductSerial'][$j]['serial_no'])? $this->data['CustStockIn'][$i]['ProductSerial'][$j]['serial_no']: 'N/A'; ?></td>
                                    <td><?php echo !empty($this->data['CustStockIn'][$i]['ProductSerial'][$j]['other_information'])? $this->data['CustStockIn'][$i]['ProductSerial'][$j]['other_information']: 'N/A'; ?></td>
                                    <td class="text-right"><?php echo $this->data['CustStockIn'][$i]['ProductSerial'][$j]['price']; ?></td>
                                </tr>
                                <?php endfor; ?>
                            </tbody>
                        </table>
                    </div>
                    <div id="popupIndex<?php echo $i; ?>"></div>
                </td>
            </tr>
                <?php endfor; ?>
        </tbody>
        <?php else : $count = count($this->data['DR']) // mode edit?>
        <tbody>
                <?php for($i = 0; $i < $count; ++$i) : ?>
            <tr class="<?php echo (($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'; ?>">
                <td><span><?php echo $i + 1; ?></span></td>
                <td>
                            <?php echo $html->link($this->data['DR'][$i]['Product']['part_no'],
                            '/products/view/' . $this->data['DR'][$i]['product_id'] . '/' . strtotime('now'),
                            array('target' => '_blank')); ?>
                </td>
                <td><?php echo $this->data['DR'][$i]['Product']['title']; ?></td>
                <td class="text-right"><?php echo $this->data['DR'][$i]['return_qty']; ?></td>
                <td class="center"><?php echo $this->data['DR'][$i]['Product']['Unit']['title']; ?></td>
                <td class="center">
                    <?php echo $ajax->link($html->image('icon/popup.png', array('alt' => 'Index information')),
                        array('controller' => 'products_serials', 'action' => 'popup_view', $this->data['DR'][$i]['product_id'] . '/its_stock_in_id:' . $this->data['DR'][$i]['id'] . '/its_stock_in_type:' . DR::ITS_STOCK_IN_TYPE),
                        array('update' => 'content', 'position' => 'bottom', 'indicator' => 'indicator' ), null, false); ?>
                </td>
            </tr>
                <?php endfor; ?>
        </tbody>
        <?php endif; ?>
    </table>
</fieldset>
<fieldset>
    <table>
        <tr>
            <td width="50px" class="top"><?php echo $form->label('DRH.remark'); ?></td>
            <td><?php echo $form->textarea('DRH.remark', array('cols' => 80, 'rows' => 3)); ?></td>
        </tr>
    </table>
</fieldset>
<?php echo $form->end(); ?>

<script type="text/javascript">
<?php if ($mode == 'add') : ?>
    function addRowListener(row) {
        $("DR" + row + "Index").observe('click', function(event) {
            popupIndex(row);
        });
        initPopUpIndex(row);

        $("DR" + row + "LocationId").observe('change', function(event) {
            $j('#Index' + row + ' Select').val($j(this).val());
        })

        $j('#Index' + row + ' Select').bind("change", function() {
                $("DR" + row + "SeeIndex").show();
                $("DR" + row + "LocationId").disable();
        });
    }

    $j(function() {
        <?php if (!empty($this->data)) : ?>
            var n = $j('#tbodyDR > tr').length;
            for (var i = 0; i < n; i++) {
                addRowListener(i);
            }
        <?php endif; ?>
        $j('#DRHDispatchId').bind('change', function(event) {
            requestUpdater('content', 'dispatch_returns/view/dispatch_id:' + $j(this).val());
        })
    });
<?php endif; ?>
</script>

<?php //debug($this->data); ?>